/**
 * webpack config
 */

const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const GenerateDependPlugin = require('./src/plugins/GenerateDependPlugin');
const webpack = require('webpack');

module.exports = {
    entry: {
        app: './src/main.ts'
    },
    resolve: {
        extensions: ['.ts', '.js', '.json'],
        alias: {
            '@': path.resolve(__dirname, 'src/')
        }
    },
    resolveLoader: {
        modules: [
            path.resolve(__dirname, './src/loaders'),
            'node_modules'
        ]
    },
    output: {
        filename: 'bundle.js',
        chunkFilename: '[name].bundle.js',
        path: path.resolve(__dirname, 'public'),
    },
    module: {
        rules: [
            {
                test: /\.(ts|tsx)?$/,
                include: path.resolve(__dirname, 'src'),
                use: [{ loader: 'ts-loader', options: { transpileOnly: true } }]
            }
        ]
    },
    plugins: [
        new CleanWebpackPlugin(),
        new HtmlWebpackPlugin({ template: './src/template.html' }),
        new CopyWebpackPlugin([
            {
                from: path.resolve(__dirname, 'src/static'),
                to: path.resolve(__dirname, 'public/static'),
                ignore: ['.*']
            }
        ]),
        new webpack.NamedModulesPlugin(),
        new webpack.HotModuleReplacementPlugin(),
        // new GenerateDependPlugin({ test: /\.(png|jpg)$/, dst: path.resolve(__dirname, 'public') }) GUI中屏蔽depen构建
    ],
    optimization: {
        splitChunks: {
            cacheGroups: {
                commons: {
                    chunks: "initial",
                    name: "common", // 打包后的文件名，任意命名
                    minChunks: 2,//最小引用2次
                    minSize: 0 // 只要超出0字节就生成一个新包
                },
                vendor: {   // 抽离第三方插件
                    test: /node_modules/,   // 指定是node_modules下的第三方包
                    chunks: 'initial',
                    name: 'vendor',  // 打包后的文件名，任意命名
                    // 设置优先级，防止和自定义的公共代码提取时被覆盖，不进行打包
                    priority: 10
                }
            }
        }
    },
    devServer: {
        hot: true,
        contentBase: path.join(__dirname, "public"), //静态文件根目录
        port: 9090, // 端口
        host: 'localhost',
        overlay: true,
        compress: true // 服务器返回浏览器的时候是否启动gzip压缩
    }
};